package com.ruoyi.gczl.bzgf.cl.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gczl.bzgf.cl.domain.Bzgfcl;
import com.ruoyi.gczl.bzgf.cl.mapper.BzgfclMapper;
import com.ruoyi.gczl.bzgf.cl.service.IBzgfclService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标准规范测量Service业务层处理
 *
 * @author huacai
 * @date 2024-08-27
 */
@Service
public class BzgfclServiceImpl implements IBzgfclService {
    @Resource
    private BzgfclMapper bzgfclMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询标准规范测量
     *
     * @param bzgfclId 标准规范测量主键
     * @return 标准规范测量
     */
    @Override
    public Bzgfcl selectBzgfclByBzgfclId(Integer bzgfclId) {
        return bzgfclMapper.selectBzgfclByBzgfclId(bzgfclId);
    }

    /**
     * 查询标准规范测量列表
     *
     * @param bzgfcl 标准规范测量
     * @return 标准规范测量
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<Bzgfcl> selectBzgfclList(Bzgfcl bzgfcl) {
        return bzgfclMapper.selectBzgfclList(bzgfcl);
    }

    /**
     * 新增标准规范测量
     *
     * @param bzgfcl 标准规范测量
     * @return 结果
     */
    @Override
    public int insertBzgfcl(Bzgfcl bzgfcl) {
        bzgfcl.setCreateTime(DateUtils.getNowDate());
        return bzgfclMapper.insertBzgfcl(bzgfcl);
    }

    /**
     * 批量新增标准规范测量
     *
     * @param bzgfcls 标准规范测量List
     * @return 结果
     */
    @Override
    public int batchInsertBzgfcl(List<Bzgfcl> bzgfcls) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(bzgfcls)) {
            try {
                for (int i = 0; i < bzgfcls.size(); i++) {
                    int row = bzgfclMapper.insertBzgfcl(bzgfcls.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == bzgfcls.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改标准规范测量
     *
     * @param bzgfcl 标准规范测量
     * @return 结果
     */
    @Override
    public int updateBzgfcl(Bzgfcl bzgfcl) {
        bzgfcl.setUpdateTime(DateUtils.getNowDate());
        return bzgfclMapper.updateBzgfcl(bzgfcl);
    }

    /**
     * 批量删除标准规范测量
     *
     * @param bzgfclIds 需要删除的标准规范测量主键
     * @return 结果
     */
    @Override
    public int deleteBzgfclByBzgfclIds(Integer[] bzgfclIds) {
        return bzgfclMapper.deleteBzgfclByBzgfclIds(bzgfclIds);
    }

    /**
     * 删除标准规范测量信息
     *
     * @param bzgfclId 标准规范测量主键
     * @return 结果
     */
    @Override
    public int deleteBzgfclByBzgfclId(Integer bzgfclId) {
        return bzgfclMapper.deleteBzgfclByBzgfclId(bzgfclId);
    }
}
